/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cas.struts.action;

import com.cas.dbaccess.CASDBAccessfactory;
import com.cas.hibernate.entity.Doctor;
import com.cas.hibernate.entity.Patient;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;

import java.util.Date;
import org.apache.struts.util.LabelValueBean;

/**
 *
 * @author Libin
 */
public class PatientAction extends org.apache.struts.action.Action {
      /* forward name="success" path="" */
    private static final String SUCCESS = "viewPatients";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String forward = "viewPatients";
        if(isCancelled(request))
        {
            return mapping.findForward(forward);
        }
        
        DynaValidatorActionForm dForm = (DynaValidatorActionForm)form;
        String actionType = dForm.getString("entityAction");
        System.out.println("PatientAction........" + actionType);
        if("edit".equalsIgnoreCase(actionType))
        {
            forward = editPatient(dForm);
        }else if("add".equalsIgnoreCase(actionType))
        {
            forward = addPatient(dForm);
        }
        
        return mapping.findForward(forward);
    }
    
    private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    
    private String editPatient(DynaValidatorActionForm form)
    {
        String patientId = form.getString("id");
        Patient p = CASDBAccessfactory.getCASDbAccess().getPatient(patientId);
        p.setRegistrationNumber(form.getString("registrationNumber"));
        p.setFirstName(form.getString("firstName"));
        p.setSecondName(form.getString("secondName"));  
        p.setFamilyName(form.getString("familyName"));
        p.setDateOfBirth(parseDate(form.getString("dateOfBirth")));
        p.setNationality(form.getString("nationality"));
        
        String sNum = form.getString("omanIdCardNumber");
        if(sNum != null && sNum.trim().length() > 0)
        {
            p.setOmanIdCardNumber(new BigDecimal(sNum));
        }
        else
        {
            p.setOmanIdCardNumber(null);
        }
        p.setSex(form.getString("sex").equals("1") ? 1 : 2);
        CASDBAccessfactory.getCASDbAccess().savePatient(p);
        return "viewPatients";   
    }
    
    private String addPatient(DynaValidatorActionForm form)
    {
        Patient p = new Patient();
        p.setRegistrationNumber(form.getString("registrationNumber"));
        p.setFirstName(form.getString("firstName"));
        p.setSecondName(form.getString("secondName"));  
        p.setFamilyName(form.getString("familyName"));
        p.setDateOfBirth(parseDate(form.getString("dateOfBirth")));
        p.setNationality(form.getString("nationality"));
        p.setOmanIdCardNumber(new BigDecimal(form.getString("omanIdCardNumber").length()== 0? "0":form.getString("omanIdCardNumber")));
        p.setSex(form.getString("sex").equals("1") ? 1 : 2);
        CASDBAccessfactory.getCASDbAccess().createPatient(p);
        return "viewPatients";   
    }
    
    public Date parseDate(String dateStr)
    {
        try {
            return sdf.parse(dateStr);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
}
